#coding:utf-8
__author__ = 'Administrator'
from django.core.management.base import BaseCommand, CommandError
from Nodecontrol.models import node_info
import urllib2
import json

class Command(BaseCommand):
    help = 'Run the simple command'

    def add_arguments(self, parser):
        pass

    def handle(self, *args, **options):
        node_list = node_info.objects.all()
        for i in node_list:
            #print('rrrrrrrrrrrrrrrrrrrrrr'+i.NodeIP)
            #token = '895b82a5ae8c0b6eb06b563a0d4f3a42'
            ip = i.NodeIP
            token = i.token
            try:
                jsonres = urllib2.urlopen('http://%s:3000/ocdn/status?token=%s'%(ip,token),timeout=2).read()  #怎么提高效率
            except:
                jsonres = False
                tmp = {}
                tmp['result'] = False
            if jsonres:
                tmp = json.loads(jsonres)
            if tmp['result']:
                node_info.objects.filter(id=i.id).update(Status = "online",Mem_per = tmp['data']['memory']['mem']['per'],cpu_free = tmp['data']['cpu']['status'],recv_rate0 = tmp['data']['network']['em1']['count']['transmit-bytes-speed'],send_rate0 = tmp['data']['network']['em1']['count']['receive-bytes-speed'])
            else:
                node_info.objects.filter(id=i.id).update(Disk_total = 'bbbbb')
        self.stdout.write("task done")